package com.wanmait.recruitment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruitment.mapper.CollectionMapper;
import com.wanmait.recruitment.mapper.ConfigMapper;
import com.wanmait.recruitment.pojo.Collection;
import com.wanmait.recruitment.pojo.Job;
import com.wanmait.recruitment.pojo.Userinfo;
import com.wanmait.recruitment.service.CollectionService;
import com.wanmait.recruitment.service.ConfigService;
import com.wanmait.recruitment.util.PageContext;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class CollectionServiceImpl implements CollectionService {
    @Resource
    private CollectionMapper collectionMapper;
    @Resource
    private ConfigMapper configMapper;
    @Override
    @CacheEvict(value = "collectionCache",allEntries = true)
    public void insert(Collection collection) {
        collectionMapper.insert(collection);
    }

    @Override
    public void delete(Integer id) {
    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public void update(Collection object) {

    }

    @Override
    public Collection findById(Integer id) {
        return null;
    }

    @Override
    public List<Collection> findAll() {
        return null;
    }

    @Override
    public List<Collection> findAll(Collection object) {
        return null;
    }

    @Override
    public PageInfo<Collection> findPage(int pageNum, int pageSize) {
        return null;
    }

    @Override
    public Collection findByJobAndUserinfo(Job job, Userinfo userinfo) {
        return collectionMapper.selectByJobAndUserinfo(job,userinfo);
    }

    @Override
    @CacheEvict(value = "collectionCache",allEntries = true)
    public void deleteByColl(Collection collection) {
        collectionMapper.deleteByColl(collection);
    }

    @Override
    @Cacheable("collectionCache")
    public PageInfo<Collection> findByUserinfo(Userinfo userinfo) {
        Integer pageNum = PageContext.getPageNum();
        Integer pageSize = 0;
        try {
            pageSize = Integer.parseInt(configMapper.getValueByKey("userinfo_collection_count"));
        } catch (NumberFormatException e) {
            pageSize = Integer.parseInt(configMapper.getDefaultValueByKey("userinfo_collection_count"));
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Collection> collections = collectionMapper.selectByUserinfo(userinfo);
        PageInfo<Collection> pageInfo = new PageInfo<>(collections);
        return pageInfo;
    }
}
